package leetcode.list;

import datastructure.tree.Node;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description: https://leetcode.cn/problems/copy-list-with-random-pointer/
 * @Author Ammar
 * @Create 2023/7/23 13:45
 */
public class _138_复制带随机指针的链表 {
    Map<Node, Node> cachedNode = new HashMap<Node, Node>();

    public Node copyRandomList(Node head) {
        if (head == null) {
            return null;
        }
        if (!cachedNode.containsKey(head)) {
            Node headNew = new Node(head.val);
            cachedNode.put(head, headNew);
            headNew.next = copyRandomList(head.next);
            headNew.random = copyRandomList(head.random);
        }
        return cachedNode.get(head);
    }
}
